package com.polylink.sys.control;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.RedirectView;

import com.common.base.Constant;
import com.polylink.sys.service.IUserService;

/**
 *@author xiliang.xiao
 *@date 2015年3月20日 下午5:11:31
 *
 **/
@Controller
public class Login {
	@Autowired
	private IUserService userService;
	
	@RequestMapping("toIndex")
	public String toLogin(HttpServletRequest request){
		String language = request.getParameter("language");
		if(StringUtils.isNotBlank(language)){
			request.getSession().setAttribute("language", language);
			//设置spring i18n国际化
			request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,  new Locale(language));
		}else{
			request.getSession().setAttribute("language", "zh_CN");
			//设置spring i18n国际化
			request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("zh_CN"));
		}
		return "../toIndex";
	}
			
	@RequestMapping("login")
	public ModelAndView login(
			HttpServletRequest req){
		String exceptionClassName = (String)req.getAttribute("shiroLoginFailure");
        String error = null;
        if(UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "1";
        } else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "1";
        } else if(AuthenticationException.class.getName().equals(exceptionClassName)){
        	error= "1";
        } else if (AuthorizationException.class.getName().equals(exceptionClassName)){
        	error = "2";
        } else if(exceptionClassName != null) {
            error = "3";
        }
        if(StringUtils.isNotBlank(exceptionClassName)){
        	Logger.getLogger(getClass()).error(exceptionClassName);
        }
        ModelAndView mv = new ModelAndView("../../login");
        mv.addObject("error", error);
		return mv;
	}
	
	@RequestMapping("index")
	public String index(
			HttpServletRequest request,Model model){
		String userName = (String) SecurityUtils.getSubject().getPrincipal();
		model.addAttribute("userName", userName);
		request.getSession().setAttribute(Constant.USER_SESSION_KEY, userService.findUserByUsername(userName));
		return "../index";
	}
	@RequestMapping("/")
	public ModelAndView loginSuccess(
			HttpServletRequest request){
		ModelAndView mv = new ModelAndView(new RedirectView("index"));
		return mv;
	}
	
	@RequestMapping("error")
	public ModelAndView error(
			HttpServletRequest request){
		ModelAndView mv = new ModelAndView("../error");
		String userName = (String) SecurityUtils.getSubject().getPrincipal();
		mv.addObject("userName", userName);
		return mv;
	}
}
